import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddBook {
    public static void main(String[] args) throws SQLException {
        //1.获取数据源
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("12345678");

        //2.获得连接
        Connection connection= (Connection) dataSource.getConnection();
        //3.组装sql 获得执行器
        String addSql="insert into booklist values (?,?,?,?,?)";
        PreparedStatement statement=connection.prepareStatement(addSql);
        //填充占位符
        statement.setInt(1,5);
        statement.setString(2,"三体");
        statement.setString(3,"刘慈欣");
        statement.setBigDecimal(4, BigDecimal.valueOf(99.9));
        statement.setString(5,"科幻小说");

        //执行sql
        int result=statement.executeUpdate();
        System.out.println("执行结果:"+result);
        //关闭资源
        statement.close();
        connection.close();
    }
}
